package com.cebbank.sz.cebtemplate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cebbank.sz.cebtemplate.model.po.User;
import com.cebbank.sz.cebtemplate.service.UserService;
import com.cebbank.sz.cebtemplate.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jaco on 2019/11/28.
 */
@RestController
public class HomeController {

	private final Logger logger=LoggerFactory.getLogger(HomeController.class);

	@Autowired
//	@Qualifier("userServiceCachedImpl")
	private UserService userService;

	@RequestMapping("/")
	public String home(){
		String result=userService.showUsers();
		logger.info("hello world!");
		return result;
	}

	@RequestMapping("/savebatch/{count}")
	public String  saveBatch(@PathVariable("count") int count) {
		List<User> list = new ArrayList<User>();
		// 待添加（用户）数据
		if (count<=0) {
			count=10000;
		}
		for (int i = 0; i < count; i++) {
			User user = new User();
			user.setName("test:"+i);
			user.setAge(i);
			user.setEmail(user.getName()+"@abc.com");
			list.add(user);
		}
		// 批量插入
		long start=System.currentTimeMillis();
		boolean j=userService.saveBatch(list);
		long cost=System.currentTimeMillis()-start;
		String result=count+" records cost "+cost+" ms!";
		System.out.println(result);
		return result;
	}

	@RequestMapping("/selectPage/{pageNo}/{pageSize}")
	public String  selectPage(@PathVariable("pageNo") int pageNo,@PathVariable("pageSize") int pageSize) {
		if (pageNo <=0||pageNo >5000) {
			pageNo=1;
		}
		if (pageSize <=0||pageSize > 200 ) {
			pageSize=200;
		}
		long start=System.currentTimeMillis();
		List<User> pageList = userService.selectUserList(pageNo, pageSize);
		long cost=System.currentTimeMillis()-start;
		int count=pageList.size();
		pageList=null;
		String result=count+" records cost "+cost+" ms!";
		System.out.println(result);
		return new Integer(count).toString();
	}

	@RequestMapping("/selectPageSlow/{pageNo}/{pageSize}")
	public String  selectPageSlow(@PathVariable("pageNo") int pageNo,@PathVariable("pageSize") int pageSize) {
		if (pageNo <=0||pageNo >5000) {
			pageNo=1;
		}
		if (pageSize <=0||pageSize > 200 ) {
			pageSize=200;
		}
		long start=System.currentTimeMillis();
		IPage<User> pageList = userService.selectPage(pageNo, pageSize);
		long cost=System.currentTimeMillis()-start;
		int count=(int)pageList.getSize();
		pageList=null;
		String result=count+" records cost "+cost+" ms!";
		System.out.println(result);
		return new Integer(count).toString();
	}

}
